import { _decorator, Component, Vec2, game, find, instantiate, Collider2D, v3 } from 'cc';
const { ccclass, property } = _decorator;

// import EventMng from "./EventMng";
import GameSc from "./GameSc";

@ccclass('BallSc')
export default class BallSc extends Component {
    @property
    BallIndex: number = 1;
    //    /** 不要用它检查是否结束游戏 */
    isNotCheckOver = false;
    //    /** 是不是丢出去的,影响声音处理 */
    isDrop = false;
    start() {
        // this.getComponent(PhysicsCollider).restitution = 0.1;
        this.getComponent(Collider2D).restitution = 0.1;
    }
    calculationDis(localPos: Vec2, tarPos: Vec2) {

        let dx = localPos.x - tarPos.x;

        let dy = localPos.y - tarPos.y;

        let dis = Math.sqrt(dx * dx + dy * dy);
        return dis;
    }
    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact) {
        if (GameSc.Instance._isReplay) {
            return;
        }
        if (otherCollider.getComponent(BallSc)) {
            this.isNotCheckOver = true;
            if (otherCollider.getComponent(BallSc).BallIndex == selfCollider.getComponent(BallSc).BallIndex) {
                // 看看是不是球
                let target = selfCollider;
                let other = otherCollider;
                if (otherCollider.node.position.y <= selfCollider.node.position.y) {
                    target = otherCollider;
                    other = selfCollider;
                }
                if (find('Canvas/GameSc').getComponent(GameSc).BallLevelUp(target.node, other.node)) {
                    other.node.removeComponent(BallSc);
                }
                find('Canvas/GameSc').getComponent(GameSc).combo(this.BallIndex)
            }
            if (this.BallIndex == 0) {
                let target = selfCollider;
                let other = otherCollider;
                if (otherCollider.node.position.y <= selfCollider.node.position.y) {
                    target = otherCollider;
                    other = selfCollider;
                }
                if (find('Canvas/GameSc').getComponent(GameSc).BallLevelUp(target.node, other.node)) {
                    other.node.removeComponent(BallSc);
                }
            }
            if (this.BallIndex == -1) {

                // const arr =find("Canvas/GameSc").getComponent(GameSc).ballsLayer.getComponentsInChildren(BallSc);
                // for (let index = arr.length-1; index > 0; index--) {
                //     if(this.calculationDis(v2(otherCollider.node.position),v2(selfCollider.node.position))<200){
                //         arr[index].node.destroy();
                //     }
                // }

                let boomtx = instantiate(find("Canvas/GameSc").getComponent(GameSc).boomtx)

                find("Canvas/GameSc").getComponent(GameSc).ballsLayer.addChild(boomtx);
                // boomtx.x = this.node.x;
                // boomtx.y = this.node.y;
                boomtx.position = v3(this.node.position.x, this.node.position.y);


                this.node.destroy();
                otherCollider.node.destroy();
            }
        } else if (!this.isDrop) {
            //仅响一次
            this.isDrop = true;
            game.emit('game-ball-drop');
        }
    }
}